package com.anjbo.aspect;

import com.anjbo.common.RespDataObject;
import com.anjbo.common.RespHelper;
import com.anjbo.common.RespStatusEnum;
import com.anjbo.exception.ResponseException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class ResponseAspect {


    @Around("@annotation(com.anjbo.aspect.ResultWrapped)")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        RespDataObject<Object> respData = new RespDataObject<Object>();
        try {
            Object retVal = pjp.proceed();
            RespHelper.setSuccessDataObject(respData,retVal);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            RespHelper.setFailDataObject(respData,null,throwable instanceof ResponseException?throwable.getMessage(): RespStatusEnum.FAIL.getMsg());
        }
        return respData;
    }
}
